package org.nobject.common.swing.g;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;

import javax.swing.JPanel;

/**
 * GPanel
 *
 * @author bianrongjun
 * @version 1.0
 */
public class GPanel extends JPanel implements IG{
	
	/** bgImage */
	private Image backgroundImage;
	
	/**
	 * 构造函数 
	 */
	public GPanel() {
		super();
		this.core=new GComponent();
	}
	
	/**
	 * 构造函数
	 * @param container 父容器
	 * @param
	 * @param name 名称
	 * @param constraints 布局约束
	 * @param preferredSize 首选大小
	 * @param layoutManager 布局管理
	 * @return JPanel
	 */
	public GPanel(Container container,String name,Object constraints,Dimension preferredSize,Dimension offset,LayoutManager layoutManager){
		this();
		//this.setAutoscrolls(autoscrolls)scrolls)(autoStretch);
		if(name!=null) this.setName(name);
		if(preferredSize!=null){
			if(preferredSize.width<0) preferredSize.width=container.getWidth();
			if(preferredSize.height<0) preferredSize.height=container.getHeight();
			this.setPreferredSize(preferredSize);
		}
		if(offset==null){
			offset=new Dimension(-1,-1);
		}
		
		if(container!=null&&offset.width<0&&preferredSize!=null) offset.width=(container.getWidth()-preferredSize.width)/2;
		if(container!=null&&offset.height<0&&preferredSize!=null) offset.height=(container.getHeight()-preferredSize.height)/2;
		
		//添加完毕
		if(container!=null){
			if(constraints==null)container.add(this);
			else container.add(this,constraints);
		}
		
		
		this.setBounds(offset.width,offset.height,this.getPreferredSize().width,this.getPreferredSize().height);
		if(layoutManager!=null)this.setLayout(layoutManager);
		
	}
	
	/**
	 * 构造函数 
	 * @param preferredSize
	 */
	public GPanel(Dimension preferredSize){
		this(null,null,null,preferredSize,null,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param preferredSize
	 */
	public GPanel(Container container){
		this(container,null,null,null,null,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param preferredSize
	 */
	public GPanel(Container container,Dimension preferredSize){
		this(container,null,null,preferredSize,null,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param preferredSize
	 */
	public GPanel(Container container,Dimension preferredSize,LayoutManager layoutManager){
		this(container,null,null,preferredSize,null,layoutManager);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param preferredSize
	 * @param layoutManager
	 * @param comps
	 */
	public GPanel(Container container,Dimension preferredSize,LayoutManager layoutManager,Component[] comps){
		this(container,null,null,preferredSize,null,layoutManager);
		this.add(comps);
	}
	
	/**
	 * 构造函数 
	 * @param preferredSize
	 * @param layoutManager
	 */
	public GPanel(Dimension preferredSize,LayoutManager layoutManager){
		this(null,null,null,preferredSize,null,layoutManager);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 */
	public GPanel(Container container,String name){
		this(container, name,null,null,null,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 * @param preferredSize
	 */
	public GPanel(Container container,String name,Dimension preferredSize){
		this(container,name,null,preferredSize,null,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 * @param constraints
	 * @param preferredSize
	 * @param layoutManager
	 */
	public GPanel(Container container,String name,Object constraints,Dimension preferredSize,LayoutManager layoutManager){
		this(container, name,constraints,preferredSize,null,layoutManager);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 * @param preferredSize
	 * @param layoutManager
	 */
	public GPanel(Container container,String name,Dimension preferredSize,LayoutManager layoutManager){
		this(container,name,null,preferredSize,null,layoutManager);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param dclass
	 * @param name
	 * @param constraints
	 * @param layoutManager
	 */
	public GPanel(Container container,String name,Object constraints,LayoutManager layoutManager){
		this(container,name,constraints,null,null,layoutManager);
	}
	
	/** GComponent */
	private GComponent core;
	public GComponent getGCore() {return core;}
	
	
	/** getBackgroundImage */
	public Image getBackgroundImage() {
		return backgroundImage;
	}

	/** setBackgroundImage */
	public void setBackgroundImage(Image backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	public void paintChildren(Graphics g) {core.paintChildren(g);super.paintChildren(g);}

	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(this.backgroundImage!=null) g.drawImage(this.backgroundImage, 0, 0,this.getPreferredSize().width,this.getPreferredSize().height,null);
	}
	
	/** 
	 * add
	 * @param comps
	 */
	public void add(Component[] comps){
		for(Component component:comps){
			this.add(component);
		}
	}
}
